package com.taobao.action.menu;

import org.eclipse.swt.widgets.Widget;

import com.taobao.action.CommonAction;
import com.taobao.factory.TBRunnable;
import com.taobao.pojo.Config;
import com.taobao.pojo.RowObject;
import com.taobao.pojo.TBMessage;
import com.taobao.tools.TBMessageDialog;
import com.taobao.ui.widget.StatusWidget;

public abstract class CommonMenuAction extends CommonAction {

	@Override
	public void doAction(Object uiObject) {
		final Widget commonWidget = (Widget) uiObject;
		final RowObject rowObject = getRowObject();
		if (rowObject != null) {
			before();
			Thread thread = new Thread(new TBRunnable() {
				public void run() {
					final TBMessage tbMessage = process(rowObject);
					commonWidget.getDisplay().asyncExec(new Runnable() {
						public void run() {
							after();
							TBMessageDialog.messageHandler(commonWidget.getDisplay().getActiveShell(), tbMessage);
						}
					});
				}
			});
			thread.setName(getActionName());
			thread.start();
		}
	}

	public abstract RowObject getRowObject();

	public abstract String setBeforeMessage();

	public abstract String setAfterMessage();

	public abstract TBMessage process(RowObject rowObject);

	public void before() {
		StatusWidget statusWidget = (StatusWidget) Config.uiContext.get(StatusWidget.ID);
		statusWidget.showOrHide(true);
		Config.tbStatusLineManager.setMessage(setBeforeMessage());
	}

	public void after() {
		StatusWidget statusWidget = (StatusWidget) Config.uiContext.get(StatusWidget.ID);
		statusWidget.showOrHide(false);
		Config.tbStatusLineManager.setAutoMessage(setAfterMessage());
	}

}
